package com.icss.ah.shop.action;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.bean.SyncSelectTree;
import com.icss.ah.base.util.ResourceLoader;
import com.icss.ah.shop.service.ShopApplyService;
import com.icss.j2ee.util.UUID;

/**
 * 店铺申请Action
 * @author sw
 *
 */
@Controller
@Scope("prototype")
public class ShopApplyAction extends BaseAction {
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	private String shopId;
	private String shopName;
	private String shopAddr;
	private String shopTel;
	private String shopArea;
	private String idcardF;
	private String idcardB;
	private String shopLogo;
	private String shopPic;
	private String shopDesc;
	private String payOnline;
	private String legalPerson;
	private String businessLicense;
	private String businessScope;
	private String bankCode;
	private String ProvinceCode;
	private String cityCode;
	private String bankAccountName;
	private String bankAccount;
	private String level;
	private String shopProvince;
	private String shopCity;
	private String shopCounty;
	private String startMoney;
	
	public String toShopPage(){
		String returnPage = "toShopPage";
		request = this.getRequest();
		String corpId = getBaseCorpId();
		//获取是否开店信息
		String userCode = (String) request.getSession().getAttribute("userCode");
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("userCode", userCode);
		map.put("corpId", corpId);
		HashMap accountShop = shopService.queryAccountMap(map);
		String applyStatus = accountShop.get("APPLY_STATUS").toString();
		HashMap shopMap = new HashMap();
		String approveStatus = "";
		String areaName = "";
		String areaCode = "";
		if("1".equals(applyStatus)){
			map.put("shopId", accountShop.get("SHOP_ID").toString());
			shopMap = shopService.queryShopMap(map);
			approveStatus = shopMap.get("REGISTER_STATUS") == null ? "" : shopMap.get("REGISTER_STATUS").toString();
//			returnPage = "toShopApproveStatus";
			List<Map> areaCodes = shopManageService.queryCheckList(map);
			if(areaCodes.size() > 0){
				for (Map aMap : areaCodes) {
					areaCode += aMap.get("AREA_CODE").toString() + ",";
					areaName += aMap.get("ANAME").toString() + ",";
				}
				if(!"".equals(areaCode)){
					areaCode = areaCode.substring(0,areaCode.length() - 1);
					areaName = areaName.substring(0,areaName.length() - 1);
				}
			}
		}
		List bankList = shopService.queryBankList();
		HashMap hm = new HashMap();
		hm.put("level", "2");
		List provinceList = shopService.queryProvinceCityList(hm);
		hm.put("areaPcode", shopMap.get("SHOP_PROVINCE"));
		hm.put("level", "3");
		List cityList = shopService.queryProvinceCityList1(hm);
		hm.put("areaPcode", shopMap.get("SHOP_CITY"));
		hm.put("level", "4");
		List countyList = shopService.queryProvinceCityList1(hm);
		request.setAttribute("shopMap", shopMap);
		request.setAttribute("bankList", bankList);
		request.setAttribute("areaCode", areaCode);
		request.setAttribute("areaName", areaName);
		request.setAttribute("provinceList", provinceList);
		request.setAttribute("cityList", cityList);
		request.setAttribute("countyList", countyList);
		request.setAttribute("approveStatus", approveStatus);
		
		return returnPage;
	}
	
	public String toStatementPage(){
		return "toStatementPage";
	}
	
	/**
	 * 初始版本
	 */
	
	
	/*public String toShopPage(){
		String returnPage = "toShopPage";
		request = this.getRequest();
		//获取是否开店信息
		String userCode = (String) request.getSession().getAttribute("userCode");
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("userCode", userCode);
		HashMap accountShop = shopService.queryAccountMap(map);
		String applyStatus = accountShop.get("APPLY_STATUS").toString();
		HashMap shopMap = new HashMap();
		String approveStatus = "";
		if("1".equals(applyStatus)){
			map.put("shopId", accountShop.get("SHOP_ID").toString());
			shopMap = shopService.queryShopMap(map);
			approveStatus = shopMap.get("REGISTER_STATUS") == null ? "" : shopMap.get("REGISTER_STATUS").toString();
//			returnPage = "toShopApproveStatus";
		}
		List bankList = shopService.queryBankList();
		HashMap hm = new HashMap();
		hm.put("level", "2");
		List provinceList = shopService.queryProvinceCityList(hm);
		request.setAttribute("shopMap", shopMap);
		request.setAttribute("bankList", bankList);
		request.setAttribute("provinceList", provinceList);
		request.setAttribute("approveStatus", approveStatus);
		
		return returnPage;
	}*/
	/**
	 * 获取省市编码
	 * @return
	 */
	public String queryAreaList(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String areaPcode = request.getParameter("areaPcode");
			areaPcode = (areaPcode == null || "".equals(areaPcode)) ? "" : areaPcode;
			
			HashMap<String,Object> map = new HashMap<String,Object>();
			map.put("areaPcode", areaPcode);
			map.put("level", level);
			List cityList = shopService.queryProvinceCityList(map);
			
			jsonText.setSuccess(true);
			jsonText.setMsg("提交成功！");
			jsonText.setObj(cityList);
		} catch (Exception e) {
			jsonText.setMsg("提交失败！");
			jsonText.setSuccess(false);
			errlog.error("Login|异常：",e);
		}
		return "jsonText";
	}
	public String queryAreaList1(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String areaPcode = request.getParameter("areaPcode");
			areaPcode = (areaPcode == null || "".equals(areaPcode)) ? "" : areaPcode;
			
			HashMap<String,Object> map = new HashMap<String,Object>();
			map.put("areaPcode", areaPcode);
			map.put("level", level);
			List cityList = shopService.queryProvinceCityList1(map);
			
			jsonText.setSuccess(true);
			jsonText.setMsg("提交成功！");
			jsonText.setObj(cityList);
		} catch (Exception e) {
			jsonText.setMsg("提交失败！");
			jsonText.setSuccess(false);
			errlog.error("Login|异常：",e);
		}
		return "jsonText";
	}
	/**
	 * 保存店铺信息
	 * @return
	 */
	public String saveShop(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String corpId = getBaseCorpId();
			shopId = (shopId == null || "".equals(shopId)) ? "" : shopId;
			String userCode = (String) request.getSession().getAttribute("userCode");
			String approveStatus = request.getParameter("approveStatus");
			approveStatus = (approveStatus == null || "".equals(approveStatus)) ? "0" : approveStatus;
			if("2".equals(approveStatus)){
				approveStatus = "0";//审核不通过再次提交，状态改为待审核
			}
			
			HashMap<String,Object> map = new HashMap<String,Object>();
			map.put("corpId", corpId);
			map.put("shopName", shopName);
			map.put("shopAddr", shopAddr);
			map.put("shopTel", shopTel);
			map.put("idcardF", idcardF);
			map.put("idcardB", idcardB);
			map.put("shopLogo", shopLogo);
			map.put("shopPic", shopPic);
			map.put("shopDesc", shopDesc);
			map.put("payOnline", payOnline);
			map.put("legalPerson", legalPerson);
			map.put("businessLicense", businessLicense);
			map.put("businessScope", businessScope);
			map.put("bankCode", bankCode);
			map.put("ProvinceCode", ProvinceCode);
			map.put("cityCode", cityCode);
			map.put("bankAccountName", bankAccountName);
			map.put("bankAccount", bankAccount);
			map.put("registerStatus", approveStatus);
			map.put("shopProvince", shopProvince);
			map.put("shopCity", shopCity);
			map.put("shopCounty", shopCounty);
			
			startMoney = startMoney == null||startMoney.equals("") ? "0":startMoney;
			DecimalFormat format = new DecimalFormat("0.00");
			startMoney = format.format(Double.valueOf(startMoney));
			
			map.put("startMoney", startMoney);
			
			//保存店铺信息
			if("".equals(shopId)){
				map.put("shopId", new UUID().toString());
				shopService.insertShop(map);
			}else {
				map.put("shopId", shopId);
				shopService.updateShop(map);
			}
			//保存店铺经营区域信息
			if(!"".equals(shopArea)){//经营区域不为空，则添加经营区域信息
				shopManageService.deleteShopArea(map);
				String areas[] = shopArea.split(",");
				for (String area : areas) {
					map.put("areaCode", area);
					map.put("shopAreaUuid", new UUID().toString());
					shopManageService.insertShopArea(map);
				}
			}
			//更新店铺登录信息表
			map.put("applyStatus", 1);
			map.put("userCode", userCode);
			shopService.updateAccountShop(map);
			
			jsonText.setSuccess(true);
			jsonText.setMsg("提交成功！");
		} catch (Exception e) {
			jsonText.setMsg("提交失败！");
			jsonText.setSuccess(false);
			errlog.error("Login|异常：",e);
		}
		return "jsonText";
	}
	
	/**
	 * 判断用户名是否存在
	 */
	public void checkShopNameExist(){
		jsonText = new Json();
		try {
			boolean result = shopService.checkShopNameExist(shopName);
			jsonText.setSuccess(result);
		} catch (Exception e) {
			e.printStackTrace();
			jsonText.setSuccess(false);
			jsonText.setMsg(e.getMessage());
			errlog.error("Login|异常：",e);
		}
		str2Resp(JSONObject.fromObject(jsonText).toString());
	}
	/**
	 * 判断对公账户是否正确
	 */
	public void checkBankAccount(){
		jsonText = new Json();
		try {
			boolean result = false;
			bankAccount = bankAccount == null ? "" : bankAccount;
			if(bankAccount.length()>6){
				HashMap hm = new HashMap();
				hm.put("bankCode", bankCode);
				hm.put("bankAccountBin", bankAccount.substring(0, 6));
				hm.put("bankAccount", bankAccount);
				result = shopService.checkBankAccount(hm);
			}
			jsonText.setSuccess(result);
		} catch (Exception e) {
			e.printStackTrace();
			jsonText.setSuccess(false);
			jsonText.setMsg(e.getMessage());
			errlog.error("Login|异常：",e);
		}
		str2Resp(JSONObject.fromObject(jsonText).toString());
	}
	
//	/**
//	 * 获取公众号资源树数据
//	 */
//	public void getTree(){
//		request = getRequest();
//		//id 异步加载展开树时传到后台的id
//		//tarEnt 入口编码，以编码为入口编码的节点作为根节点
//		//ifAll 是否一次性展示所有数据，1是 0否
//		String id = request.getParameter("id");
//		id = id == null ? "" : id;
//		String ifAll = request.getParameter("ifAll");
//		ifAll = ifAll == null ? "0" : ifAll;
//		String corpId = request.getParameter("corpId");
//		corpId = corpId == null ? "" : corpId;
//		String agentId = request.getParameter("agentId");
//		agentId = agentId == null ? "" : agentId;
//		String userId = request.getParameter("userId");
//		userId = userId == null ? "" : userId;
//		SyncSelectTree selectTree = new SyncSelectTree();
//		selectTree.setId(id);
//		selectTree.setIfAll(ifAll);
//		List<SyncSelectTree> list = shopService.getTree(selectTree);
//		HashMap map = new HashMap();
//		map.put("corpId", corpId);
//		map.put("agentId", agentId);
//		map.put("userId", userId);
//		List<Map> checkList = new ArrayList();
//		if("".equals(userId)){
//			checkList = shopService.queryModelListTem("ShopApplyBean.queryCheckList", map);
//		} else {
//			checkList = shopService.queryModelListTem("ShopApplyBean.queryCheckUserList", map);
//		}
//		checkList = checkList == null ? new ArrayList() : checkList;
//		for (SyncSelectTree sst : list) {
//			sst.setOpen(true);
//			String sstUuid = sst.getUuid();
//			if(!checkList.isEmpty()){
//				for (Map m : checkList) {
//					String mUuid = m.get("RESOURCE_UUID") == null ? "" : m.get("RESOURCE_UUID").toString();
//					if(sstUuid.equals(mUuid)){
//						sst.setChecked(true);
//					}
//				}
//			}
//		}
//		str2Resp(JSONArray.fromObject(list).toString());
//	}

	public String getShopId() {
		return shopId;
	}

	public void setShopId(String shopId) {
		this.shopId = shopId;
	}

	public String getShopName() {
		return shopName;
	}

	public void setShopName(String shopName) {
		this.shopName = shopName;
	}

	public String getShopAddr() {
		return shopAddr;
	}

	public void setShopAddr(String shopAddr) {
		this.shopAddr = shopAddr;
	}

	public String getShopTel() {
		return shopTel;
	}

	public void setShopTel(String shopTel) {
		this.shopTel = shopTel;
	}

	public String getIdcardF() {
		return idcardF;
	}

	public void setIdcardF(String idcardF) {
		this.idcardF = idcardF;
	}

	public String getIdcardB() {
		return idcardB;
	}

	public void setIdcardB(String idcardB) {
		this.idcardB = idcardB;
	}

	public String getShopPic() {
		return shopPic;
	}

	public void setShopPic(String shopPic) {
		this.shopPic = shopPic;
	}

	public String getShopDesc() {
		return shopDesc;
	}

	public void setShopDesc(String shopDesc) {
		this.shopDesc = shopDesc;
	}

	public Json getJsonText() {
		return jsonText;
	}

	public void setJsonText(Json jsonText) {
		this.jsonText = jsonText;
	}
	public String getLegalPerson() {
		return legalPerson;
	}
	public void setLegalPerson(String legalPerson) {
		this.legalPerson = legalPerson;
	}
	public String getBusinessLicense() {
		return businessLicense;
	}
	public void setBusinessLicense(String businessLicense) {
		this.businessLicense = businessLicense;
	}
	public String getBusinessScope() {
		return businessScope;
	}
	public void setBusinessScope(String businessScope) {
		this.businessScope = businessScope;
	}
	public String getBankCode() {
		return bankCode;
	}
	public void setBankCode(String bankCode) {
		this.bankCode = bankCode;
	}
	public String getProvinceCode() {
		return ProvinceCode;
	}
	public void setProvinceCode(String provinceCode) {
		ProvinceCode = provinceCode;
	}
	public String getCityCode() {
		return cityCode;
	}
	public void setCityCode(String cityCode) {
		this.cityCode = cityCode;
	}
	public String getBankAccountName() {
		return bankAccountName;
	}
	public void setBankAccountName(String bankAccountName) {
		this.bankAccountName = bankAccountName;
	}
	public String getBankAccount() {
		return bankAccount;
	}
	public void setBankAccount(String bankAccount) {
		this.bankAccount = bankAccount;
	}

	public String getShopArea() {
		return shopArea;
	}

	public void setShopArea(String shopArea) {
		this.shopArea = shopArea;
	}

	public String getShopLogo() {
		return shopLogo;
	}

	public void setShopLogo(String shopLogo) {
		this.shopLogo = shopLogo;
	}

	public String getPayOnline() {
		return payOnline;
	}

	public void setPayOnline(String payOnline) {
		this.payOnline = payOnline;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getShopProvince() {
		return shopProvince;
	}

	public void setShopProvince(String shopProvince) {
		this.shopProvince = shopProvince;
	}

	public String getShopCity() {
		return shopCity;
	}

	public void setShopCity(String shopCity) {
		this.shopCity = shopCity;
	}

	public String getShopCounty() {
		return shopCounty;
	}

	public void setShopCounty(String shopCounty) {
		this.shopCounty = shopCounty;
	}

	public String getStartMoney() {
		return startMoney;
	}

	public void setStartMoney(String startMoney) {
		this.startMoney = startMoney;
	}
	
}
